package com.example.yls.demoa;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    SQLiteDatabase db;
    String sql;
    TextView txtData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtData = findViewById(R.id.show_stu);

        findViewById(R.id.create_sqlite_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createDataBase();
            }
        });
        findViewById(R.id.create_stulist_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createStudentBase();
            }
        });
        findViewById(R.id.insert_studata_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insertStudentData();
            }
        });
        findViewById(R.id.show_studata_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                readDataFromDb();
            }
        });
    }

    private void readDataFromDb() {
        if (db==null){
            createDataBase();
        }
        Cursor cursor = db.query("student",
                null,
                null,
                null,
                null,
                null,
                null);
        if(cursor==null||cursor.getCount()==0){
            return;
        }
        StringBuffer buffer = new StringBuffer();
        for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            int stuid = cursor.getInt(0);
            String name = cursor.getString(1);
            int age = cursor.getInt(2);
            buffer.append(stuid).append("   ").append(name).append("   ").append(age).append("   ").append("\n");

        }
        txtData.setText(buffer);
    }

    private void insertStudentData() {
        if (db==null){
            createDataBase();
        }
        //第一种：
       /* String sql = "insert into student values (171001,'fankaibin',23)";
        db.execSQL(sql);*/
        //第二种：
        ContentValues values = new ContentValues();
        values.put("stuid",171002);
        values.put("name","渣渣辉");
        values.put("age",28);
        db.insert("student",null,values);
    }

    private void createStudentBase() {
        if (db==null){
            createDataBase();
        }
        sql = "create table student(stuid integer,name varchar(20),age integer)";
        db.execSQL(sql);

    }

    private void createDataBase() {
        String path = getFilesDir().getAbsolutePath()+ File.separator+"stu.db";
        Log.e("MainActivity","path=  "+path);
        db = SQLiteDatabase.openOrCreateDatabase(path,null);
    }
}
